package com.csii.epaySandbox.serviceImpl;

import javax.annotation.Resource;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.csii.epaySandbox.dao.UserDao;
import com.csii.epaySandbox.entity.User;
import com.csii.epaySandbox.exception.PasswordException;
import com.csii.epaySandbox.exception.UserNotFoundException;
import com.csii.epaySandbox.service.UserService;
@Service("userService")
public class UserServiceImpl implements UserService {
//	@Resource
//	private UserDao userDao;
	@Value("#{system.pwdSalt}")
	private String salt;
	
	@Value("#{system.username}")
	private String nameDB;
	
	@Value("#{system.password}")
	private String pwdDB;
	
	public User login(String username, String password) throws UserNotFoundException, PasswordException {

		if(username == null || username.trim().isEmpty()){
			throw new UserNotFoundException("用户名为空");
		}
		
		if(password == null || password.trim().isEmpty()){
			throw new PasswordException("密码为空");
		}
		
//		User user = userDao.findUserByName(username.trim());
		
		if(!username.equals(nameDB)){
			throw new UserNotFoundException("该用户不存在");
		}
		
		String pwd = DigestUtils.md5Hex(salt+password.trim());

		if(!pwd.equals(pwdDB.trim())){
			throw new PasswordException("密码错误");
		}
		return new User(username,password);
	}

}
